package com.xoa.service.financialManage.Impl;

import com.itextpdf.kernel.pdf.filters.IFilterHandler;
import com.xoa.dao.financialManage.IncomeExpensePlanTypeMapper;
import com.xoa.model.financialManage.IncomeExpensePlanType;
import com.xoa.model.financialManage.IncomeExpenseRecords;
import com.xoa.model.inv.OmShipmentExample;
import com.xoa.service.financialManage.IncomeExpensePlanTypeService;
import com.xoa.util.ToJson;
import com.xoa.util.common.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class IncomeExpensePlanTypeServiceImpl implements IncomeExpensePlanTypeService {

    @Autowired
    IncomeExpensePlanTypeMapper incomeExpensePlanTypeMapper;

    //查询计划类别列表
    @Override
    public ToJson fundIncome(HttpServletRequest request,IncomeExpensePlanType income) {
        ToJson toJson=new ToJson(1,"err");
        try {
            toJson.setFlag(0);
            toJson.setObj(incomeExpensePlanTypeMapper.selectByExample(null));
            toJson.setMsg("OK");
        }catch (Exception e){
            e.getMessage();
        }
        return toJson;
    }

    //根据计划类别ID查询
    @Override
    public ToJson findIncomTypeById(HttpServletRequest request, IncomeExpensePlanType incomeExpensePlanType) {
        ToJson toJson=new ToJson(1,"err");
        try {
            toJson.setFlag(0);
            toJson.setObject(incomeExpensePlanTypeMapper.selectByPrimaryKey(incomeExpensePlanType.getPlanTypeId()));
            toJson.setMsg("OK");
        }catch (Exception e){
            e.getMessage();
        }
        return toJson;
    }

    //计划类别新增和修改
    @Override
    public ToJson saveIncomType(HttpServletRequest request, IncomeExpensePlanType incomeExpensePlanType) {
        ToJson toJson=new ToJson(1,"err");
        try {
            int istrue=0;
            //只判断ID是否为空
            if (incomeExpensePlanType.getPlanTypeId()!=null){
                istrue=incomeExpensePlanTypeMapper.updateByPrimaryKeySelective(incomeExpensePlanType);
            }else{
                istrue=incomeExpensePlanTypeMapper.insertSelective(incomeExpensePlanType);
            }
            if (istrue>0){
                toJson.setFlag(0);
                toJson.setMsg("OK");
            }

        }catch (Exception e){
            e.getMessage();
        }
        return toJson;
    }

    //删除计划类别
    @Override
    public ToJson deleteIncomType(HttpServletRequest request, IncomeExpensePlanType incomeExpensePlanType) {
        ToJson toJson=new ToJson(1,"err");
        try {
            int istrue=incomeExpensePlanTypeMapper.deleteByPrimaryKey(incomeExpensePlanType.getPlanTypeId());
            if (istrue>0){
                toJson.setFlag(0);
                toJson.setMsg("OK");
            }
        }catch (Exception e){
            e.getMessage();
        }
        return toJson;
    }

    @Override
    public ToJson IncomCount(HttpServletRequest request, String year, String month, String quarter, String planId) {
        ToJson toJson=new ToJson(1,"err");
        try {
            if ("0".equals(month)){
                month="";
            }
            int count=0;
            if (StringUtils.checkNull(year)){
                year="2020";
                count=12;
            }
            if (!StringUtils.checkNull(year)){
                count=12;
            }
            if (!StringUtils.checkNull(month)){
                Calendar a = Calendar.getInstance();
                a.set(Calendar.YEAR, Integer.parseInt(year));
                a.set(Calendar.MONTH, Integer.parseInt(month) - 1);
                a.set(Calendar.DATE, 1);
                a.roll(Calendar.DATE, -1);
                int maxDate = a.get(Calendar.DATE);
                count=maxDate;
            }
            Map<String,Object> map=new HashMap<>();
            map.put("recordYear",year);
            if (!StringUtils.checkNull(planId)){
                map.put("planId",planId);
            }
            if (!StringUtils.checkNull(month)){
                map.put("record",month);
            }
            List<IncomeExpenseRecords> incomeExpenseRecords=new ArrayList<>();
            if (StringUtils.checkNull(month)){
                incomeExpenseRecords = incomeExpensePlanTypeMapper.selectInComCount(map);
            }
            if (!StringUtils.checkNull(quarter)){
                count=4;
                incomeExpenseRecords = incomeExpensePlanTypeMapper.selectInComQuarterCount(map);
            }
            if (!StringUtils.checkNull(month)){
                map.put("recordMonth",month);
                incomeExpenseRecords = incomeExpensePlanTypeMapper.selectInComMonthCount(map);
            }
            List<IncomeExpenseRecords> list=new ArrayList<>();
            IncomeExpenseRecords omShipmentExample5=new IncomeExpenseRecords();


            if (StringUtils.checkNull(omShipmentExample5.getInTotalComeRecord() +"")||"null".equals(omShipmentExample5.getInTotalComeRecord()+"")){
                omShipmentExample5.setInTotalComeRecord(0.00);
            }
            if (StringUtils.checkNull(omShipmentExample5.getExpenseTotalRecord() +"")||"null".equals(omShipmentExample5.getExpenseTotalRecord()+"")){
                omShipmentExample5.setExpenseTotalRecord(0.00);
            }
            if (StringUtils.checkNull(omShipmentExample5.getCountTotalRecord())){
                omShipmentExample5.setCountTotalRecord("0");
            }
            if (StringUtils.checkNull(omShipmentExample5.getChaTotalRecord() +"")||"null".equals(omShipmentExample5.getChaTotalRecord()+"")){
                omShipmentExample5.setChaTotalRecord(0.00);
            }
            for (int i=1;i<=count;i++){
                int s=0;
                int e=0;
                IncomeExpenseRecords omShipmentExample4=new IncomeExpenseRecords();
                for (IncomeExpenseRecords omShipmentExample: incomeExpenseRecords){
                    if ((i+"").equals(omShipmentExample.getRecordCountMonth())){
                        omShipmentExample4.setInComeRecord(omShipmentExample.getInComeRecord());
                        s++;
                    }
                }
                for (IncomeExpenseRecords omShipmentExample1: incomeExpenseRecords){
                    if ((i+"").equals(omShipmentExample1.getRecordCountMonth())){
                        omShipmentExample4.setExpenseRecord(omShipmentExample1.getExpenseRecord());
                        e++;
                    }
                }
                if (e==0){
                    omShipmentExample4.setExpenseRecord(0.00);
                }
                if (s==0){
                    omShipmentExample4.setInComeRecord(0.00);
                }
                if (e>0 || s>0){
                    omShipmentExample4.setCountRecord("1");
                }else{
                    omShipmentExample4.setCountRecord("0");
                }
                omShipmentExample4.setRecordCountMonth(i+"");
                omShipmentExample4.setChaRecord(omShipmentExample4.getInComeRecord()-omShipmentExample4.getExpenseRecord());
                list.add(omShipmentExample4);
                omShipmentExample5.setInTotalComeRecord(omShipmentExample5.getInTotalComeRecord()+omShipmentExample4.getInComeRecord());
                omShipmentExample5.setExpenseTotalRecord(omShipmentExample5.getExpenseTotalRecord()+omShipmentExample4.getExpenseRecord());
                omShipmentExample5.setChaTotalRecord(omShipmentExample5.getChaTotalRecord()+omShipmentExample4.getChaRecord());
                if (e>0 || s>0){
                    omShipmentExample5.setCountTotalRecord((Integer.parseInt(omShipmentExample5.getCountTotalRecord())+1)+"");
            }
            }
            //list.add(omShipmentExample5);
                toJson.setObj(list);
                toJson.setObject(omShipmentExample5);
                toJson.setFlag(0);
                toJson.setMsg("OK");
        }catch (Exception e){
         e.printStackTrace();
        }
        return toJson;
    }
    @Override
    public ToJson incomYearCount(HttpServletRequest request) {
        ToJson toJson=new ToJson(1,"err");
        try {
            Map<String,Object> map=new HashMap<>();
            int year=2016;
            List<IncomeExpenseRecords> list=new ArrayList<>();

            for (int i=0;i<5;i++){
                int x=0;
                map.put("recordYear",year);
                List<IncomeExpenseRecords> incomeExpenseRecords=new ArrayList<>();
                incomeExpenseRecords = incomeExpensePlanTypeMapper.selectInComYearCount(map);
                for (IncomeExpenseRecords incomeExpenseRecords1:incomeExpenseRecords){
                    if ((year+"").equals(incomeExpenseRecords1.getRecordCountMonth())){
                        list.add(incomeExpenseRecords1);
                        x++;
                    }
                }
                if (x==0){
                    IncomeExpenseRecords incomeExpenseRecords1=new IncomeExpenseRecords();
                    incomeExpenseRecords1.setInComeRecord(0.0);
                    incomeExpenseRecords1.setExpenseRecord(0.0);
                    incomeExpenseRecords1.setRecordCountMonth(year+"");
                    list.add(incomeExpenseRecords1);
                }
                year++;
            }
            //list.add(omShipmentExample5);
            toJson.setObj(list);
            toJson.setFlag(0);
            toJson.setMsg("OK");
        }catch (Exception e){
            e.printStackTrace();
        }
        return toJson;
    }

}
